package com.sojson.controller.po;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.controller.po.param.verify.CommentParamVerify;
import com.sojson.entity.po.Comment;
import com.sojson.entity.vo.CommentVo;
import com.sojson.enums.core.EAuditStatus;
import com.sojson.enums.core.EModuleNo;
import com.sojson.result.Result;
import com.sojson.service.CommentService;

import io.swagger.annotations.Api;

/**
 * 商品评论表控制器
 * 
 * @author liu
 * @date 2021-03-16
 */
@Api(tags = "商品评论表管理")
@RestController
@RequestMapping(value = "/comment")
public class CommentController {

    @Resource
    private CommentService commentServiceImpl;

    /**
     * 单条插入
     * 
     * @param comment
     * @return
     * @throws Exception 
     */
    @PostMapping("/insert")
    @PreAuthorize("@ss.login()")
    public Object insert(@RequestBody Comment comment) throws Exception {
        // 校验参数
        Result<?> paramResult = CommentParamVerify.insert(comment);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        comment.insertInit();
        comment.setNoModule(EModuleNo.SHOPPING_MALL.getCode());
        commentServiceImpl.insert(comment);
        return Result.success();
    }

    /**
     * 单条插入
     * 
     * @param comment
     * @return
     * @throws Exception 
     */
    @PostMapping("/insertHotel")
    @PreAuthorize("@ss.login()")
    public Object insertHotel(@RequestBody Comment comment) throws Exception {
        // 校验参数
        Result<?> paramResult = CommentParamVerify.insert(comment);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        comment.insertInit();
        comment.setNoModule(EModuleNo.HOTEL.getCode());
        commentServiceImpl.insertHotel(comment);
        return Result.success();
    }

    /**
     * 批量插入
     * 
     * @param comment
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/insertList", method = RequestMethod.POST)
    public Object insertList(@RequestBody Comment comment) throws SQLException {
        return Result.success(commentServiceImpl.insertList(comment));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param comment
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/deleteByDto", method = RequestMethod.POST)
    public Object deleteByDto(@RequestBody Comment comment) throws SQLException {
        return Result.success(commentServiceImpl.deleteByDto(comment));
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/countAll", method = RequestMethod.GET)
    public Object countAll() throws SQLException {
        int countAll = commentServiceImpl.countAll();
        return Result.success(countAll);
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public Object findAll() throws SQLException {
        List<Comment> findAll = commentServiceImpl.findAll();
        return Result.success(findAll);
    }

    /**
     * 根据对象统计数量
     * 
     * @param comment
     * @return
     * @throws SQLException
     */
    @GetMapping("/countByDto")
    public Object countByDto(Comment comment) throws SQLException {
        int countByDto = commentServiceImpl.countByDto(comment);
        return Result.success(countByDto);
    }

    /**
     * 根据对象统计数量
     * 
     * @param comment
     * @return
     * @throws SQLException
     */
    @GetMapping("/countAppByDto")
    public Object countAppByDto(Comment comment) throws SQLException {
        comment.setStatus(EAuditStatus.SUCCESS.getCode());
        int countByDto = commentServiceImpl.countByDto(comment);
        return Result.success(countByDto);
    }

    /**
     * 根据对象查询
     * 
     * @param comment
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findByDto", method = RequestMethod.POST)
    public Object findByDto(@RequestBody Comment comment) throws SQLException {
        List<Comment> findByDto = commentServiceImpl.findByDto(comment);
        return Result.success(findByDto);
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public Object deleteById(Integer id) throws SQLException {
        commentServiceImpl.deleteById(id);
        return Result.success();
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/deleteByIdList", method = RequestMethod.POST)
    public Object deleteByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        return Result.success(commentServiceImpl.deleteByIdList(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param comment
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public Object updateById(@RequestBody Comment comment) throws SQLException {
        commentServiceImpl.updateById(comment);
        return Result.success();
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param comment
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateStaticByIdList", method = RequestMethod.POST)
    public Object updateStaticByIdList(@RequestBody Comment comment) throws SQLException {
        return Result.success(commentServiceImpl.updateStaticByIdList(comment));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param comment
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateChangeByIdList", method = RequestMethod.POST)
    public Object updateChangeByIdList(@RequestBody Comment comment) throws SQLException {
        return Result.success(commentServiceImpl.updateChangeByIdList(comment));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param comment
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updatePartialById", method = RequestMethod.POST)
    public Object updatePartialById(@RequestBody Comment comment) throws SQLException {
        commentServiceImpl.updatePartialById(comment);
        return Result.success();
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param comment
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateStaticPartialByIdList", method = RequestMethod.POST)
    public Object updateStaticPartialByIdList(@RequestBody Comment comment) throws SQLException {
        return Result.success(commentServiceImpl.updateStaticPartialByIdList(comment));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param comment
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateChangePartialByIdList", method = RequestMethod.POST)
    public Object updateChangePartialByIdList(@RequestBody Comment comment) throws SQLException {
        return Result.success(commentServiceImpl.updateChangePartialByIdList(comment));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findById", method = RequestMethod.GET)
    public Object findById(Integer id) throws SQLException {
        Comment findById = commentServiceImpl.findById(id);
        return Result.success(findById);
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findByIdList", method = RequestMethod.GET)
    public Object findByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        ArrayList<Comment> findByIdList = commentServiceImpl.findByIdList(ids);
        return Result.success(findByIdList);
    }

    /**
     * 通过对象查询id列表
     * 
     * @param comment
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findIdByDto", method = RequestMethod.POST)
    public Object findIdByDto(@RequestBody Comment comment) throws SQLException {
        ArrayList<Integer> findIdByDto = commentServiceImpl.findIdByDto(comment);
        return Result.success(findIdByDto);
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/clear", method = RequestMethod.POST)
    public Object clear() throws SQLException {
        commentServiceImpl.clear();
        return Result.success();
    }

    /**
     * 审核评论信息
     * 
     * @param comment
     * @return
     * @throws SQLException
     */
    @PutMapping(value = "/audit")
    @PreAuthorize("@ss.hasPermi('/comment/audit')")
    public Object audit(@RequestBody Comment comment) throws SQLException {
        // 校验参数
        Result<?> paramResult = CommentParamVerify.audit(comment);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        commentServiceImpl.audit(comment);
        return Result.success();
    }

    /**
     * 根据对象查询(包含其他附加内容)
     * 
     * @param comment
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/list")
    public Object list(Comment comment) throws SQLException {
        comment.setStatus(EAuditStatus.SUCCESS.getCode());
        List<CommentVo> findByDto = commentServiceImpl.list(comment);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 通过id查询(包含其他附加内容)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/getInfo/{id}")
    public Object getInfo(@PathVariable Integer id) throws SQLException {
        CommentVo getInfo = commentServiceImpl.getInfo(id);
        return Result.success(getInfo);
    }

    /**
     * 根据对象查询(包含其他附加内容)
     * 
     * @param comment
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/arrayGoods")
    @PreAuthorize("@ss.hasPermi('/comment/array')")
    public Object arrayGoods(CommentVo comment) throws SQLException {
        comment.setNoModule(EModuleNo.SHOPPING_MALL.getCode());
        List<CommentVo> findByDto = commentServiceImpl.arrayGoods(comment);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 根据对象查询(包含其他附加内容)
     * 
     * @param comment
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/arrayHotel")
    @PreAuthorize("@ss.hasPermi('/comment/array')")
    public Object arrayHotel(CommentVo comment) throws SQLException {
        comment.setNoModule(EModuleNo.HOTEL.getCode());
        List<CommentVo> findByDto = commentServiceImpl.arrayHotel(comment);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 通过id查询(包含其他附加内容)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/getMsg")
    @PreAuthorize("@ss.hasPermi('/comment/getMsg')")
    public Object getMsg(Integer id) throws SQLException {
        CommentVo getInfo = commentServiceImpl.getMsg(id);
        return Result.success(getInfo);
    }

}